/**
 * 
 */
package wong.board.client.ui;

import java.util.Date;

import wong.board.client.util.JSONTool;
import wong.board.shared.ContentKeys;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.HasText;

/**
 * @author wong
 * 
 */
public class TitlePresenter extends Presenter<TitlePresenter.View, JSONObject> {

	private String timeStamp;
	private String titleText;

	public TitlePresenter() {
		this((View) GWT.create(View.class));
	}

	public TitlePresenter(View view) {
		super(view);
	}

	@Override
	public void updateView(JSONObject title) {
		DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd' 'HH:mm:ss");// yyyyMMdd'-'HH:mm:ss.SSS
		if (timeStamp == null) {
			timeStamp = "[" + format.format(new Date())
					+ "] - (v@VERSION_MAJOR@.@VERSION_MINOR@.@BUILD_NUMBER@)";
		}

		String titleTextString;
		JSONString newTitleText = JSONTool.isString(title, ContentKeys.TitleText);
		if (newTitleText != null) {
			if (titleText == null) {
				titleText = newTitleText.stringValue();
			} else {
				titleText += " & " + newTitleText.stringValue();
			}
			titleTextString = titleText + " " + timeStamp;
		} else {
			titleTextString = timeStamp;
		}

		getView().getTitleText().setText(titleTextString);
	}

	public interface View extends Presenter.View {

		public HasText getTitleText();
	}

}
